package fhb.mobile.todoapp.datamodell;

import java.util.ArrayList;

public class ToDoItem {
	
	private long createDate = 0;
	private String title = "";
	private String mail = "";
	private String content = "";
	private long deadline = 0;
	private boolean featured = false;
	private boolean completed = false;
	private long lastChange = 0;
	private ArrayList<Integer> associatedContacts = new ArrayList<Integer>();
	
	public ToDoItem(String title, String content, long deadline, boolean featured) {
		setCreateDate(System.currentTimeMillis());
		setTitle(title);
		setContent(content);
		setDeadline(deadline);
		setFeatured(featured);
		setLastChange(0);
		setAssociatedContacts(null);
	}
	
	public ToDoItem(String title, String content, long deadline, boolean featured, ArrayList<Integer> associatedContacts) {
		setCreateDate(0);
		setTitle(title);
		setContent(content);
		setDeadline(deadline);
		setFeatured(featured);
		setLastChange(0);
		setAssociatedContacts(associatedContacts);
	}

	public ToDoItem(long createDate, String title, String content, long deadline, boolean featured, boolean completed, long lastChange, ArrayList<Integer> associatedContacts) {
		setCreateDate(createDate);
		setTitle(title);
		setContent(content);
		setDeadline(deadline);
		setFeatured(featured);
		setCompleted(completed);
		setLastChange(lastChange);
		setAssociatedContacts(associatedContacts);
	}

	public long getCreateDate() {
		return createDate;
	}

	public void setCreateDate(long createDate) {
		this.createDate = (createDate != 0) ? createDate : System.currentTimeMillis();
	}
	
	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = (title != null) ? title : "Kein Titel (" + createDate + ")";
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = (content != null) ? content : new String();
	}

	public long getDeadline() {
		return deadline;
	}

	public void setDeadline(long deadline) {
		this.deadline = (deadline != 0) ? deadline : System.currentTimeMillis();
	}

	public boolean isFeatured() {
		return featured;
	}

	public void setFeatured(boolean featured) {
		this.featured = featured;
	}

	public boolean isCompleted() {
		return completed;
	}

	public void setCompleted(boolean completed) {
		this.completed = completed;
	}

	public long getLastChange() {
		return lastChange;
	}

	public void setLastChange(long lastChange) {
		this.lastChange = (lastChange != 0) ? lastChange : System.currentTimeMillis();
	}

	public ArrayList<Integer> getAssociatedContacts() {
		return associatedContacts;
	}

	public void setAssociatedContacts(ArrayList<Integer> associatedContacts) {
		this.associatedContacts = (associatedContacts != null) ? associatedContacts : new ArrayList<Integer>();
	}	
	
	public void addAssociatedContact(Integer contactID) {
		associatedContacts.add(contactID);
	}
	
	public String getContacts(){
		String s="";
		for (Integer wert : associatedContacts) {
			s = s + wert.toString() + "|";
		}
		return s;
	}
	
	public void setContacts(String s){
		String[] array = s.split("|");
		associatedContacts = new ArrayList<Integer>();
		for (String string : array) {
			if (s != null && s.length() > 0)
				associatedContacts.add(new Integer(string));
		}
	}
	
	public ToDoItem clone() {
		return new ToDoItem(createDate, title, content, deadline, featured, completed, lastChange, associatedContacts);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((associatedContacts == null) ? 0 : associatedContacts
						.hashCode());
		result = prime * result + (completed ? 1231 : 1237);
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result + (int) (createDate ^ (createDate >>> 32));
		result = prime * result + (int) (deadline ^ (deadline >>> 32));
		result = prime * result + (featured ? 1231 : 1237);
		result = prime * result + (int) (lastChange ^ (lastChange >>> 32));
		result = prime * result + ((mail == null) ? 0 : mail.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	public boolean equals(ToDoItem obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ToDoItem other = (ToDoItem) obj;
		if (associatedContacts == null) {
			if (other.associatedContacts != null)
				return false;
		} else if (!associatedContacts.equals(other.associatedContacts))
			return false;
		if (completed != other.completed)
			return false;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (createDate != other.createDate)
			return false;
		if (deadline != other.deadline)
			return false;
		if (featured != other.featured)
			return false;
		if (lastChange != other.lastChange)
			return false;
		if (mail == null) {
			if (other.mail != null)
				return false;
		} else if (!mail.equals(other.mail))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}	
	
	public void addContact(String id){
		associatedContacts.add(Integer.parseInt(id));
	}
	
	public boolean isRelatedContact(int id){
		if (associatedContacts == null || associatedContacts.size() == 0){
			return false;
		}
		for (Integer contactId : associatedContacts) {
			if (contactId.intValue() == id){
				return true;
			}
		}
		return false;
	}
}
